package com.example.cake.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cake.common.Result;
import com.example.cake.entity.DTO.SysCrowdDTO;
import com.example.cake.entity.DTO.SysFlavorDTO;
import com.example.cake.entity.SysClassify;
import com.example.cake.entity.SysCrowd;
import com.example.cake.entity.SysFlavor;
import com.example.cake.service.SysCrowdService;
import com.example.cake.service.SysFlavorService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/crowd")
public class SysCrowdController {
    @Autowired
    private SysCrowdService sysCrowdService;


    @PostMapping("/list")
    public Result list(@RequestBody SysCrowdDTO sysCrowdDTO) {
        LambdaQueryWrapper<SysCrowd> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(sysCrowdDTO.getName()),SysCrowd::getName,sysCrowdDTO.getName());
        Page<SysCrowd> page = new Page<>(sysCrowdDTO.getPage(), sysCrowdDTO.getLimit());
        return Result.success(sysCrowdService.page(page,lqw));
    }
    @GetMapping("/listByValue")
    public Result listByValue() {
        List<SysCrowd> list = sysCrowdService.list();
        list.forEach(item->{
            item.setLabel(item.getName());
            item.setValue(item.getId().toString());
        });
        return Result.success(list);
    }
    @GetMapping("/getNameList")
    public Result getNameList() {
        List<SysCrowd> list = sysCrowdService.list();
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysCrowd sysCrowd) {
        sysCrowd.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        sysCrowd.setUpdateTime(DateUtil.toLocalDateTime(new Date()));
        return Result.success(sysCrowdService.save(sysCrowd));
    }

    @PostMapping("/remove")
    public Result add(@RequestBody Integer id) {
        return Result.success(sysCrowdService.removeById(id));
    }

    @PostMapping("/update")
    public Result update(@RequestBody SysCrowd sysCrowd) {
        sysCrowd.setUpdateTime(DateUtil.toLocalDateTime(new Date()));
        return Result.success(sysCrowdService.updateById(sysCrowd));
    }
}
